package com.scy.java.thread;
//死锁的演示
public class DeadLockTest {
    public static void main(String[] args) {
        StringBuffer stringBuffer1 = new StringBuffer();
        StringBuffer stringBuffer2 = new StringBuffer();
        new Thread(){
            @Override
            public void run() {
                synchronized (stringBuffer1){
                    stringBuffer1.append("a");
                    stringBuffer1.append("1");
                }
                try {
                    Thread.sleep(100);
                } catch (InterruptedException e) {
                    throw new RuntimeException(e);
                }
                synchronized (stringBuffer2){
                    stringBuffer1.append("b");
                    stringBuffer1.append("2");
                    System.out.println(stringBuffer1);
                    System.out.println(stringBuffer2);
                }
                try {
                    Thread.sleep(100);
                } catch (InterruptedException e) {
                    throw new RuntimeException(e);
                }
            }
        }.start();
        new Thread(){
            @Override
            public void run() {
                synchronized (stringBuffer2){
                    stringBuffer1.append("c");
                    stringBuffer1.append("3");
                }
                try {
                    Thread.sleep(100);
                } catch (InterruptedException e) {
                    throw new RuntimeException(e);
                }
                synchronized (stringBuffer1){
                    stringBuffer1.append("d");
                    stringBuffer1.append("4");
                    System.out.println(stringBuffer1);
                    System.out.println(stringBuffer2);
                }
                try {
                    Thread.sleep(100);
                } catch (InterruptedException e) {
                    throw new RuntimeException(e);
                }
            }
        }.start();
    }
}
